﻿#include "ProjectPanel.h"
#include <QLabel>
#include <QPushButton>
#include <QGridLayout>
#include <QSettings>
#include <QLineEdit>
#include <QFileDialog>
#include <QCoreApplication>
#include <QRadioButton>
#include "../../models/ProjectData.h"
#include "../../controller/SignalsBus.h"
#include "../../controller/ImageMappingManager.h"
#include "../../controller/ImageDataProvider.h"
ProjectPanel::ProjectPanel(QWidget *parent)
    : QGroupBox(parent)
{
    mappingManager = new ImageMappingManager();
    dataProvider = new ImageDataProvider(mappingManager);
    setupUi();

    connect(mappingManager, &ImageMappingManager::mappingUpdated,
            this, &ProjectPanel::onMappingUpdated);
    connect(mappingManager, &ImageMappingManager::scanCompleted,
            this, &ProjectPanel::onScanCompleted);
    connect(mappingManager, &ImageMappingManager::parametersChanged,
            this, &ProjectPanel::onParametersChanged);

    connect(mappingManager, &ImageMappingManager::imagesRenamed,
            this, &ProjectPanel::onImagesRenamed);
}

ProjectPanel::~ProjectPanel()
{
}


void ProjectPanel::onLoadSettings()
{
    // QString settingsPath = QCoreApplication::applicationDirPath() +"config.ini";
    QString settingsFile = QFileDialog::getOpenFileName(this,tr("选择配置文件"),"/",tr("*.ini"));

    QSettings settings(settingsFile,QSettings::IniFormat);
    valuesCollection[fovRowCount]->setText(settings.value(fovRowCount).toString());
    valuesCollection[fovColCount]->setText(settings.value(fovColCount).toString());
    valuesCollection[fixtureRowCount]->setText(settings.value(fixtureRowCount).toString());
    valuesCollection[fixtureColCount]->setText(settings.value(fixtureColCount).toString());

    int _fovCount = settings.value(fovRowCount).toInt()*settings.value(fovColCount).toInt();
    valuesCollection[fovCount]->setText(QString::number(_fovCount));

    int _fixtureCount = settings.value(fixtureRowCount).toInt()*settings.value(fixtureColCount).toInt();
    valuesCollection[fixtureCount]->setText(QString::number(_fixtureCount));


    ProjectData::getInstance()->fovRowCount = settings.value(fovRowCount).toInt();
    ProjectData::getInstance()->fovColCount = settings.value(fovColCount).toInt();
    ProjectData::getInstance()->fovCount = _fovCount;
    ProjectData::getInstance()->fixtureRowCount = settings.value(fixtureRowCount).toInt();
    ProjectData::getInstance()->fixtureColCount = settings.value(fixtureColCount).toInt();
    ProjectData::getInstance()->fixtureCount = _fixtureCount;

    ProjectData::getInstance()->coordinateMap.fovRow = settings.value(fovRowCount).toInt();
    ProjectData::getInstance()->coordinateMap.fovCol = settings.value(fovColCount).toInt();
    ProjectData::getInstance()->coordinateMap.fixtureRow = settings.value(fixtureRowCount).toInt();
    ProjectData::getInstance()->coordinateMap.fixtureCol = settings.value(fixtureColCount).toInt();

    ProjectData::getInstance()->displayMode = settings.value(displayMode).toInt();

    if(ProjectData::getInstance()->displayMode==ProjectData::getInstance()->SingleImage)
    {
        modeCollection[singleImage]->setChecked(true);
        emit SignalsBus::getInstance()->onChangeDisplayMode(ProjectData::getInstance()->SingleImage);
    }else if(ProjectData::getInstance()->displayMode==ProjectData::getInstance()->MultipleImage)
    {
        modeCollection[multipleImage]->setChecked(true);
        emit SignalsBus::getInstance()->onChangeDisplayMode(ProjectData::getInstance()->MultipleImage);
    }



    emit SignalsBus::getInstance()->onLoadProject();
}

void ProjectPanel::onSaveSettings()
{
    // QString settingsPath = QCoreApplication::applicationDirPath()+"config.ini";

    QString settingsfile = QFileDialog::getSaveFileName(this,tr("保存配置文件"),"/",tr("*.ini"));
    QSettings settings(settingsfile,QSettings::IniFormat);

    int _fovCount = valuesCollection[fovRowCount]->text().toInt()*valuesCollection[fovColCount]->text().toInt();
    valuesCollection[fovCount]->setText(QString::number(_fovCount));

    int _fixtureCount = valuesCollection[fixtureRowCount]->text().toInt()*valuesCollection[fixtureRowCount]->text().toInt();
    valuesCollection[fixtureCount]->setText(QString::number(_fixtureCount));


    settings.setValue(fovRowCount,valuesCollection[fovRowCount]->text());
    settings.setValue(fovColCount,valuesCollection[fovColCount]->text());
    settings.setValue(fovCount,valuesCollection[fovCount]->text());
    settings.setValue(fixtureRowCount,valuesCollection[fixtureRowCount]->text());
    settings.setValue(fixtureColCount,valuesCollection[fixtureColCount]->text());
    settings.setValue(fixtureCount,valuesCollection[fixtureCount]->text());
    settings.setValue(displayMode,ProjectData::getInstance()->displayMode);

}

void ProjectPanel::onLoadAndScanDirectory()
{
    QString folder = QFileDialog::getExistingDirectory(this,"选择图像文件夹");

    if(!folder.isEmpty())
    {
        mappingManager->setParameters(
            valuesCollection[fovRowCount]->text().toInt(),
            valuesCollection[fovColCount]->text().toInt(),
            valuesCollection[fixtureRowCount]->text().toInt(),
            valuesCollection[fixtureColCount]->text().toInt()
        );

        mappingManager->scanImageFolder(folder);
        mappingManager->loadMappingFromConfig(folder);
        mappingManager->renameImagesForHalcon(QCoreApplication::applicationDirPath()+"/halconImage");

    }

    valuesCollection[currentFolder]->setText(folder);
}

void ProjectPanel::onMappingUpdated()
{
    emit SignalsBus::getInstance()->mappingUpdated();
}

void ProjectPanel::onScanCompleted()
{
    emit SignalsBus::getInstance()->scanCompleted();
}

void ProjectPanel::onParametersChanged()
{
    emit SignalsBus::getInstance()->parametersChanged();
}

void ProjectPanel::onImagesRenamed(bool success, const QString &message)
{
    if(success)
    {

        ProjectData::getInstance()->imageFileNameList = dataProvider->getHalconImagePaths();

        for(const auto& item:ProjectData::getInstance()->imageFileNameList)
        {
            qDebug()<<"image: "<<item;
        }
        emit SignalsBus::getInstance()->loadImageFolder(ProjectData::getInstance()->imageFileNameList);
    }
    else
        qDebug()<<"Halcon rename files failed";
}


void ProjectPanel::initComponents()
{

    // 项目信息
    auto btnScanFolder = new QPushButton(tr("加载并扫描文件夹"));
    auto lbCurrentFolder = new QLabel(tr("当前图像文件夹"));
    auto lineCurrentFolder = new QLineEdit();

    auto projectInfoLayout = new QHBoxLayout();
    projectInfoLayout->addWidget(lbCurrentFolder,1);
    projectInfoLayout->addWidget(lineCurrentFolder, 2);
    projectInfoLayout->addWidget(btnScanFolder, 2);


    // 图像信息
    auto lbSingleImagaeRowCount = new QLabel(tr("单铍窗FOV行数:"));
    auto lineSingleImageRowCount = new QLineEdit();
    auto lbSingleImageColCount = new QLabel(tr("单铍窗FOV列数:"));
    auto lineSingleImageColCount = new QLineEdit();
    auto lbSingleImageFovCount = new QLabel(tr("单铍窗FOV总数:"));
    auto lineSingleImageFovCount = new QLineEdit();
    auto lbSingleImageInfo = new QLabel(tr("项目数据设定"));

    auto imageInfoLayout = new QGridLayout();
    auto lbRowCount = new QLabel(tr("治具行数"));
    auto lbColCount = new QLabel(tr("治具列数"));
    auto lbItemCount = new QLabel(tr("治具物料总数"));
    auto lineRowCount = new QLineEdit();
    auto lineColCount = new QLineEdit();
    auto lineItemCount = new QLineEdit();

    imageInfoLayout->addWidget(lbSingleImageInfo, 0, 0,1,2);
    imageInfoLayout->addWidget(lbSingleImagaeRowCount, 1, 0,1,1);
    imageInfoLayout->addWidget(lineSingleImageRowCount, 1, 1, 1, 1);
    imageInfoLayout->addWidget(lbSingleImageColCount, 2, 0, 1, 1);
    imageInfoLayout->addWidget(lineSingleImageColCount, 2, 1, 1, 1);
    imageInfoLayout->addWidget(lbSingleImageFovCount, 3, 0, 1, 1);
    imageInfoLayout->addWidget(lineSingleImageFovCount, 3, 1, 1, 1);
    imageInfoLayout->addWidget(lbRowCount,1,2,1,1);
    imageInfoLayout->addWidget(lineRowCount,1,3,1,1);
    imageInfoLayout->addWidget(lbColCount,2,2,1,1);
    imageInfoLayout->addWidget(lineColCount,2,3,1,1);
    imageInfoLayout->addWidget(lbItemCount,3,2,1,1);
    imageInfoLayout->addWidget(lineItemCount,3,3,1,1);

    auto settingButtonLayout = new QHBoxLayout();
    auto btnSaveParameter = new QPushButton(tr("保存参数"));
    auto btnLoadParameter = new QPushButton(tr("加载参数"));

    settingButtonLayout->addWidget(btnLoadParameter);
    settingButtonLayout->addWidget(btnSaveParameter);


    auto modelSettingLayout = new QHBoxLayout();
    auto lbModel = new QLabel(tr("图像显示模式"));
    auto rbtnSingle = new QRadioButton(tr("单图显示"));
    auto rbtnMultiple = new QRadioButton(tr("多图显示"));
    modelSettingLayout->addWidget(lbModel);
    modelSettingLayout->addWidget(rbtnSingle);
    modelSettingLayout->addWidget(rbtnMultiple);

    modeCollection.insert(singleImage,rbtnSingle);
    modeCollection.insert(multipleImage,rbtnMultiple);

    connect(rbtnSingle,&QRadioButton::clicked,[](){
        ProjectData::getInstance()->displayMode=ProjectData::getInstance()->SingleImage;
        emit SignalsBus::getInstance()->onChangeDisplayMode(ProjectData::getInstance()->SingleImage);
    });

    connect(rbtnMultiple,&QRadioButton::clicked,[](){
        ProjectData::getInstance()->displayMode=ProjectData::getInstance()->MultipleImage;
        emit SignalsBus::getInstance()->onChangeDisplayMode(ProjectData::getInstance()->MultipleImage);
    });


    auto layout = new QGridLayout();

    layout->addLayout(projectInfoLayout, 0, 0, 1, 1);
    layout->addLayout(modelSettingLayout,1,0,1,1);
    layout->addLayout(imageInfoLayout, 2, 0, 1, 1);
    layout->addLayout(settingButtonLayout,3,0,1,1);

    setLayout(layout);
    setTitle(tr("项目设定"));


    connect(btnScanFolder, &QPushButton::clicked, this, &ProjectPanel::onLoadAndScanDirectory);
    connect(btnLoadParameter,&QPushButton::clicked,this,&ProjectPanel::onLoadSettings);
    connect(btnSaveParameter,&QPushButton::clicked,this,&ProjectPanel::onSaveSettings);

    valuesCollection.insert(fovColCount,lineSingleImageColCount);
    valuesCollection.insert(fovRowCount,lineSingleImageRowCount);
    valuesCollection.insert(fovCount,lineSingleImageFovCount);

    valuesCollection.insert(fixtureRowCount,lineRowCount);
    valuesCollection.insert(fixtureColCount,lineColCount);
    valuesCollection.insert(fixtureCount,lineItemCount);

    valuesCollection.insert(currentFolder,lineCurrentFolder);

    lineSingleImageFovCount->setReadOnly(true);
    lineItemCount->setReadOnly(true);

}

void ProjectPanel::setupUi()
{
    initComponents();
}

void ProjectPanel::loadDictionary(const QString &dic)
{

}

void ProjectPanel::generateFileMapping()
{

}

QString ProjectPanel::selectSettingPath()
{
    QString filepath = QFileDialog::getExistingDirectory(this,tr("选择目录"),"/");
    filepath.append("/");
    return filepath;
}

